package system

import "utility/command"

type Firewalld struct {
}

type FirewallPort struct {
	Prot     string // 端口号
	Protocol string // 协议 tcp udp all
}

/**
添加访问端口
*/
func (c *Firewalld) AddPort(port, protocol string) (err error) {

	return nil
}

func (c *Firewalld) RemovePort(port, protocol string) (err error) {

	return nil
}

/**
列出端口
*/
func (c *Firewalld) ListPort() (ports []FirewallPort, err error) {

	return
}

/**
屏蔽IP
*/
func (c *Firewalld) BlockIPs(ip string) (err error) {
	_, _, err = command.RunCmd("/bin/bash", "-c", "firewall-cmd --permanent --add-rich-rule=\"rule family=ipv4 source address="+ip+" reject\"")
	err = c.ReloadConfig()
	return nil
}

/**
重载配置
*/
func (c *Firewalld) ReloadConfig() (err error) {
	_, _, err = command.RunCmd("/bin/bash", "-c", "firewall-cmd --reload")
	return err
}

/**
移除屏蔽
*/
func (c *Firewalld) RemoveBlock(ip string) (err error) {
	_, _, err = command.RunCmd("/bin/bash", "-c", "firewall-cmd --permanent --remove-rich-rule=\"rule family=ipv4 source address="+ip+" reject\"")
	return err
}
